/*
该文件不允许出现任何与服务器，数据库做交互或者包含随机的函数，只负责计算state,
确保唯一的type只能得到唯一的state
*/
import *as types from '../actions/types'

const default_state = {
    user: {},
    status: null,
    settings: {},
    setting_fetched: false,
    isLeader: false,
}

/*
    null: 未登录
    logged_in: 已登录
    logging: 正在登录
*/

const user = (state=default_state, action) => {
    switch (action.type) {
        case types.LOGIN_SUCCESS:
            return {
                ...state, 
                status: 'logged_in',
                user: action.user
            }
        case types.LOGIN_FAILED:
            return {
                ...state,
                status: null,
                user: {}
            }
        case types.LOGINING:
            return {
                ...state,
                status: 'logging'
            }
        case types.LOGOUT:
            return {
                ...state,
                status: null,
                user: {},
                settings: {}
            }
        case types.FETCHING_SETTINGS: 
            return {
                ...state,
                setting_fetched: false
            }
        case types.FETCH_SETTINGS_SUCCESS:
            return {
                ...state,
                setting_fetched: true,
                settings: action.settings
            }
        case types.CHANGEROLE:
            return {
                ...state,
                isLeader:action.isLeader
            }
        default:
            return state
    }
}

export default user